<template>
	<view class="container">
		<view class="header">
			<text class="title">{{ editMode ? '编辑一对一预约' : '查看一对一预约' }}</text>
		</view>
		<view class="form">
			<view class="form-item">
				<text class="form-label">学生姓名：</text>
				<input v-model="appointment.studentName" :disabled="!editMode"
					class="form-input" />
			</view>
			<view class="form-item">
				<text class="form-label">预约内容：</text>
				<input v-model="appointment.appointmentContent" :disabled="!editMode"
					 class="form-input" />
			</view>
			<view class="form-item">
				<text class="form-label">预约时间：</text>
				<input v-model="appointment.appointmentTime" :disabled="!editMode"
					 class="form-input" />
			</view>
			<view class="form-item">
				<text class="form-label">金额：</text>
				<input v-model="appointment.money" :disabled="!editMode" 
					class="form-input" />
			</view>
			<view class="form-item">
				<text class="form-label">状态：</text>
				<select v-model="appointment.isFinish" :disabled="!editMode" @change="handleSelectChange($event)"
					class="form-select">
					<option value="0">未辅导</option>
					<option value="1">已辅导待确认</option>
					<option value="2">已完成</option>
				</select>
			</view>
			<view class="form-item">
				<text class="form-label">老师评价：</text>
				<input v-model="appointment.teacherComponent" :disabled="!editMode"
					 class="form-input" />
			</view>
			<button @click="toggleEditMode" class="submit-button">{{ editMode ? '保存' : '编辑' }}</button>
		</view>
	</view>
</template>

<script>
	import {
		globalVariable
	} from '../../configMy.js';
	export default {
		data() {
			return {
				appointment: {},
				editMode: false
			};
		},
		onLoad(e) {
			let appointment = JSON.parse(e.appointment);
			this.appointment = appointment;
			console.log(this.appointment);
		},
		methods: {
			toggleEditMode() {
				let that=this;
				if (this.editMode) {

					uni.request({
						url: globalVariable + "/updateAppointmentAdmin",
						data: this.appointment, // 不需要 JSON.stringify
						dataType: "json", // dataType应该是 'json'，表示期望的响应数据类型
						method:'POST',
						success: (res) => {						
							let info=res.data;
							uni.showToast({
								title:info.message
							})
						},
						fail: (err) => {
							// 请求失败的处理逻辑
							console.error("请求失败:", err);
						}
					});


					console.log("保存预约信息:", this.appointment);
					// 保存成功后，退出编辑模式
					this.editMode = false;
				} else {
					// 进入编辑模式
					this.editMode = true;
				}
			},
			handleInput(field, event) {
				this.appointment[field] = event.target.value;
			},
			handleSelectChange(event) {
				this.appointment.isFinish = event.target.value;
			}
		}
	};
</script>
<style scoped>
	.container {
		padding: 16px;
		background-color: #f5f5f5;
		/* height: 100vh; */
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.header {
		text-align: center;
		margin-bottom: 32px;
	}

	.title {
		font-size: 28px;
		font-weight: bold;
		color: #333;
	}

	.form {
		background-color: #fff;
		border-radius: 8px;
		box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
		padding: 24px;
		width: 100%;
		max-width: 500px;
	}

	.form-item {
		margin-bottom: 24px;
	}

	.form-label {
		display: block;
		font-size: 16px;
		color: #333;
		margin-bottom: 8px;
	}

	.form-select {
		width: 100%;
		padding: 12px;
		border: 1px solid #ccc;
		border-radius: 4px;
		font-size: 16px;
	}

	.form-input {
		padding: 12px;
		border: 1px solid #ccc;
		border-radius: 4px;
		font-size: 16px;
	}

	.form-input:focus,
	.form-select:focus {
		border-color: #4caf50;
		outline: none;
	}

	.submit-button {
		background-color: #4caf50;
		color: white;
		padding: 12px 20px;
		border: none;
		border-radius: 4px;
		font-size: 16px;
		cursor: pointer;
		width: 100%;
	}

	.submit-button:hover {
		background-color: #45a049;
	}
</style>